package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.common.utils.OrderUtils;
import com.pjnkj.thunder.entity.Order;
import com.pjnkj.thunder.service.OrderService;
import com.pjnkj.thunder.service.ShopService;
import com.pjnkj.thunder.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
* <p>
*  前端控制器
* </p>
 *
 * @author pjnkj
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/thunder/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    TaskService taskService;

    @Autowired
    ShopService shopService;

    /**
     * 放弃下单
     *
     * @param order
     */
    @PostMapping("fail")
    public Body fail(Integer id) {
        Order byId = orderService.getById(id);
        byId.setStatus(4);
        boolean save = orderService.saveOrUpdate(byId);
        if (save == true) {
            return Body.newInstance("弃单成功");
        }
        return Body.newInstance(Meta.CODE_400, "弃单失败");
    }

    /**
     * 确认下单
     *
     * @param order
     */
    @PostMapping("confirm")
    public Body confirm(Integer id, Integer shopid, Integer taskid) {
        Order o = new Order();
        o.setOrderid(OrderUtils.getOrderCode(id));
        o.setTaskid(taskid);
        o.setPayid(id);
        o.setShopid(shopid);
        o.setReturnmoney(taskService.getById(taskid).getReturnmoney());
        o.setMoney(taskService.getById(taskid).getMoney());
        boolean save = orderService.save(o);
        if (save == true) {
            return Body.newInstance("抢单成功");
        }
        return Body.newInstance(Meta.CODE_400, "抢单失败");
    }

    /**
     * 删除数据
     *
     * @param order
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = orderService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
    * 增加数据
    *
    * @param order
    */
    @PostMapping("create")
    public Body create(@Validated(Order.class) @RequestBody Order order) {
        Boolean flag = orderService.saveOrUpdate(order);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
    * 修改数据
    *
    * @param order
    */
    @PostMapping("update")
    public Body update(@Validated(Order.class) @RequestBody Order order) {
        Boolean flag = orderService.updateById(order);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
    * 查询数据
    *
    * @param
    */
    @PostMapping("find")
    public Body find(String id) {
        Order info = orderService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
    * 查询数据list
    *
    * @param
    */
    @PostMapping("findlist")
    public Body findlist() {
        List<Order> list = orderService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
    * 分页查询数据
    *
    * @param
    */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<Order> pg = new Page<Order>(current, size);
        IPage<Order> list = orderService.page(pg);
        return Body.newInstance(list);
    }
}
